const app = getApp<IAppOption>() as any
import { requestGET } from '../../utils/service'
import { calculateAge } from '../../utils/util'

Page({
  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    reservationDate: '',
    reservationId: '',
    physicalExaminationPackageTitle: '',
    pageData: <any>[],
    foldIndex: -1,
    showDetailModal: false,
    detailModalData: {},
    iconImgObj: {
      蛋类: 'https://report.bayajk.com/baya_health/static/temporary/蛋类.png',
      调味品: 'https://report.bayajk.com/baya_health/static/temporary/调味品.png',
      调味料: 'https://report.bayajk.com/baya_health/static/temporary/调味品.png',
      坚果类: 'https://report.bayajk.com/baya_health/static/temporary/坚果类.png',
      粮谷: 'https://report.bayajk.com/baya_health/static/temporary/粮谷.png',
      粮谷类: 'https://report.bayajk.com/baya_health/static/temporary/粮谷类.png',
      肉类: 'https://report.bayajk.com/baya_health/static/temporary/肉类.png',
      其他类: 'https://report.bayajk.com/baya_health/static/temporary/其他类.png',
      乳类豆制品类: 'https://report.bayajk.com/baya_health/static/temporary/乳类豆制品类.png',
      乳类豆制品: 'https://report.bayajk.com/baya_health/static/temporary/乳类豆制品.png',
      蔬菜: 'https://report.bayajk.com/baya_health/static/temporary/蔬菜.png',
      水产类: 'https://report.bayajk.com/baya_health/static/temporary/水产类.png',
      水果: 'https://report.bayajk.com/baya_health/static/temporary/水果.png',
      饮料类: 'https://report.bayajk.com/baya_health/static/temporary/饮料类.png',
      油类: 'https://report.bayajk.com/baya_health/static/temporary/油类.png',
      晒太阳: 'https://report.bayajk.com/baya_health/static/temporary/其他类.png',
      鱼肝油: 'https://report.bayajk.com/baya_health/static/temporary/其他类.png',
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options)
    let userInfo = wx.getStorageSync('userInfo')
    userInfo.age = calculateAge(userInfo.birthDay)
    this.setData({
      userInfo: userInfo,
      reservationId: options.reservationId,
      reservationDate: options.reservationDate,
      physicalExaminationPackageTitle: options.physicalExaminationPackageTitle,
    })
    this.getReportDetails(options.reservationId)
  },
  //获取报告详情
  getReportDetails(id: any) {
    const params = {
      reservationId: id
    }
    requestGET("physicalExaminationRecordDetail/queryByPhysicalExaminationRecordId", params)
      .then((res: any) => {
        if (res.data.code == '200') {
          let obj = res.data.data
          const mappedArray = Object.entries(obj).map(([key, value]) => {
            return { key, value };
          });
          let array = <any>[]
          mappedArray.forEach((item:any)=>{
            let model = {key:'',value:[]}
            model.key = item.key
            let list = <any>[]
            item.value.forEach((i:any)=>{
              if(i.standardDeviation<=2 && i.cellEntropy && i.cellEntropy!=0) {
                i.standardDeviation = i.standardDeviation.toFixed(3)
                i.nutritionName = i.physicalExaminationPackageBasis?.nutritionName
                const lowerBound = i.physicalExaminationPackageBasis?.lowerBound
                const upperBound = i.physicalExaminationPackageBasis?.upperBound
                i.standRed = lowerBound>0&&i.standardDeviation<=lowerBound
                i.cellRed = upperBound>0&&i.cellEntropy>=upperBound
                list.push(i)
              }
            })
            list.sort((x: any, y: any) => x.standardDeviation - y.standardDeviation);
            model.value = list
            array.push(model)
          })          
          this.setData({
            pageData: array
          })
        } else {
          wx.showToast({
            title: '服务异常',
            icon: 'error',
            duration: 2000
          })
        }
      })
  },
  // 折叠
  clickTab(e: any) {
    let index = -1
    if (e.currentTarget.dataset.index != this.data.foldIndex) {
      index = e.currentTarget.dataset.index
    }
    this.setData({
      foldIndex: index
    })
  },
  seeMore(e: any) {
    console.log('e====',e);
    let detailModalData = e.currentTarget.dataset.item
    this.setData({
      detailModalData: detailModalData,
      showDetailModal: true
    })
  },
  // 关闭弹窗
  closeModal() {
    this.setData({
      showDetailModal: false
    })
  },
})